import { Button, Label, Node, tween, Vec3, _decorator } from 'cc';
import { EventMgr } from '../../../Framework/Scripts/Managers/EventMgr';
import { UIComponent, UIMgr } from '../../../Framework/Scripts/Managers/UIMgr';
import myUtil from '../../../Framework/Scripts/Utils/myUtil';
import { constant } from '../constant';


import { GameApp } from '../GameApp';
import { UserData } from '../userData/UserData';

const { ccclass, property } = _decorator;


/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */
@ccclass('choujiang_UICtrl')
export class choujiang_UICtrl extends UIComponent {
    private zhuanpan: Node = null
    private meiyifenD: number = 360 / 6

    private playBtn: Button = null


    private data = [
        {
            //3
            index: 1,
            zhuanshi: 800,
            weight: 8000
        },

        {
            //5
            index: 2,
            zhuanshi: 2000,
            weight: 800
        },
        {
            //1
            index: 3,
            zhuanshi: 400,
            weight: 30000
        },
        {

            //6
            index: 4,
            zhuanshi: 5000,
            weight: 150
        },
        {
            //2
            index: 5,
            zhuanshi: 600,

            weight: 26000
        },

        {
            //4
            index: 6,
            zhuanshi: 1500,
            weight: 3000
        },




    ]



    onLoad() {







        this.playBtn = this.ViewNode("play_Btn").getComponent(Button)
        this.zhuanpan = this.ViewNode("yuanpan")

        for (let i = 0; i < this.zhuanpan.children.length; i++) {
            if (this.zhuanpan.children[i].getComponent(Label)) {
                this.zhuanpan.children[i].getComponent(Label).string = "x" + this.data[this.zhuanpan.children[i].name].zhuanshi

            }
        }

        this.AddButtonListener("close_btn", this, () => {
            GameApp.Instance.isBack = false
            UIMgr.Instance.DestroyUIView(this.node.name)
        })

        this.AddButtonListener("play_Btn", this, () => {
            this.startPlay()


        })

    }

    private startPlay(isOK: boolean = true) {
        console.log("参数回调：", isOK);

        if (isOK) {
            this.playBtn.interactable = false
            const nowD: number = this.zhuanpan.eulerAngles.z
            const qumoD: number = nowD % 360
            this.zhuanpan.setRotationFromEuler(0, 0, qumoD)
            const getW = myUtil.getWeightRandIndex(this.data)
            //     console.log("权重", getW);
            const d: number = (this.meiyifenD) * getW
            tween()
                .target(this.zhuanpan)
                .to(10, { eulerAngles: new Vec3(0, 0, 360 * 3 + d) }
                    , { easing: 'elasticInOut' })
                .call(() => {
                    //    console.log("获得奖励：", this.data[getW]);

                    const addNum: number = this.data[getW]["zhuanshi"]
                    GameApp.Instance.show_tip("恭喜获得：" + addNum + "金币")
                    UserData.Instance.localData["gold"] += addNum
                    UserData.Instance.saveLocalData()
                    EventMgr.Instance.Emit(constant.EVENT_NAME.upHomeTop, null)

                    this.playBtn.interactable = true

                })

                .start()
        }

    }
    start() {

    }

    update(deltaTime: number) {

    }
}


